package edu.scau.mis.controller.user;

import edu.scau.mis.core.controller.ResultEntity;
import edu.scau.mis.core.redis.RedisCache;
import edu.scau.mis.rest.entity.RestSeat;
import edu.scau.mis.rest.service.RestOrderService;
import edu.scau.mis.rest.service.RestSeatService;
import edu.scau.mis.sys.utils.HttpServletRequestUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/user")
public class SeatController {

    @Autowired
    RedisCache redisCache;

    @Autowired
    RestSeatService restSeatService;

    @Autowired
    RestOrderService restOrderService;

    @Autowired
    HttpServletRequest httpServletRequest;

    @PostMapping("/table")
    public ResultEntity chooseTable(@RequestParam("table") String seatNo){
        //获取请求头token
        String openid = HttpServletRequestUtils.getOpenid(httpServletRequest);

        //选择座位
        restSeatService.chooseSeat(openid, seatNo);

        return ResultEntity.success("操作成功");
    }

    @GetMapping("/changeTable")
    public ResultEntity changeTable(@RequestParam("table") String seatNo){
        //获取请求头token
        String openid = HttpServletRequestUtils.getOpenid(httpServletRequest);

        //切换座位
        restSeatService.changeSeat(openid, seatNo);

        return ResultEntity.success("操作成功");
    }

    @GetMapping("/all-table")
    public ResultEntity getAllTable(){
        //获取请求头token
        String openid = HttpServletRequestUtils.getOpenid(httpServletRequest);

        //为什么要openid?因为用户提交订单后餐位被占用，再次点餐这时允许该用户在这个被占用餐位点餐，而不允许其他用户点餐
        List<RestSeat> restSeatList = restSeatService.queryAllUnoccupiedSeat(openid);

        Map<String, Object> responseData = new HashMap<>();
        responseData.put("tableList", restSeatList);

        return ResultEntity.success("操作成功", responseData);

    }

}
